﻿using System;
using System.Runtime.Serialization;
using System.Collections.Generic;
using System.Xml.Serialization;

namespace Dynamic.Data.Model.Query
{
    [DataContract]
    [XmlType(Namespace="DynamicMatrix")]
    public sealed class ColumnSet : ColumnSetBase
    {
        private List<string> _attributes;
        private string _entityName;

        public ColumnSet()
        {
            this._attributes = new List<string>();
        }

        public ColumnSet(string entityName) : this()
        {
            this._entityName = entityName;
        }

        public ColumnSet(string[] attributes) : this()
        {
            this._attributes.AddRange(attributes);
        }

        public override void Accept(IQueryVisitor visitor)
        {
            visitor.Visit(this);
        }

        public override void AddColumn(string attributeName)
        {
            this._attributes.Add(attributeName);
        }

        public override void AddColumns(params string[] attributeNames)
        {
            foreach (string str in attributeNames)
            {
                this.AddColumn(str);
            }
        }

        public void RemoveAllColumns()
        {
            this._attributes.Clear();
        }

        public void RemoveColumn(string name)
        {
            for (int i = 0; i < this._attributes.Count; i++)
            {
                if (this._attributes[i].Equals(name))
                {
                    this._attributes.RemoveAt(i);
                    return;
                }
            }
        }
        
        [DataMember]
        [XmlArray, XmlArrayItem(ElementName="Attribute", IsNullable=false, Type=typeof(string))]
        public List<string> Attributes
        {
            get
            {
                return this._attributes;
            }
            set
            {
                this._attributes = value;
            }
        }

        [DataMember]
        [XmlIgnore]
        public string EntityName
        {
            get
            {
                return this._entityName;
            }
            set
            {
                this._entityName = value;
            }
        }
    }
}

